'use strict';

const Controller = require('egg').Controller;

class ArticleController extends Controller {
  async list() {
    const { ctx, app } = this;
    let id = ctx.params.id
        let data = await ctx.page('Article', {}, {
            where: {
                cate_id: id
            },
            include: [
                {
                    model: app.model.Category,
                }]
        })
        data = JSON.parse(JSON.stringify(data))
        console.log(data)
        ctx.apiSuccess( data, 'ok', 200 );
  }

  async detail() {
    const { ctx, app } = this;
    let id = ctx.params.id
        let data = await app.model.Article.findOne({
            where: {
                 id
            },
            include: [
                {
                    model: app.model.Category,
                }]
        })
        data = JSON.parse(JSON.stringify(data))
        data.content = data.content.replace(/\/public\/uploads/g, this.config.webUrl + '/public/uploads')
        ctx.apiSuccess( data, 'ok', 200 );
  }


}

module.exports = ArticleController;
